package www.study.com;

import java.util.Stack;

//有效的括号 https://leetcode.cn/problems/valid-parentheses/
public class code19 {
    public static void main(String[] args) {

    }

    class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<Character>();
            char cur = '9';
            for(int i = 0 ; i < s.length() ; i++){
                cur = s.charAt(i);
                if(cur =='(' || cur == '[' || cur == '{'){
                    stack.push(cur);
                }else{
                    if(stack.isEmpty() || !match(stack.peek(), cur)) return false;
                    stack.pop();
                }
            }
            return stack.isEmpty(); //此处要注意，不能直接返回true
        }
        public boolean match(char a, char b){
            if(a == '(' && b != ')' || a == '[' && b != ']' || a == '{' && b != '}') return false;
            return true;
        }
    }
}
